var categroys = [];
var titles = [];
var hits = [];
var btn = 0;
layui.use(['jquery','layer','table','form','upload','laydate'],function(){
    var $=layui.jquery;
    var layer=layui.layer;
    var laydate = layui.laydate;

    //绑定时间格式
    laydate.render({
        //绑定元素
        elem: '#createDate',
        //日期和时间类型
        type: 'datetime',
        //时间事件为点击事件
        trigger: 'click'
    })
    getHits();
    //按钮样式
    $('.btn_style').on('click',function () {
        $(this).addClass('btn_onclick').siblings().removeClass('btn_onclick');
        $(this).removeClass('btn_remove').siblings().addClass('btn_remove');
    })
    $('#likeNum').on('click',function () {
        btn = 0;
        getHits();
    })
    $('#shareNum').on('click',function () {
        btn = 1;
        getHits();
    })
    $('#collectNum').on('click',function () {
        btn = 2;
        getHits();
    })
    $('#lookNum').on('click',function () {
        btn = 3;
        getHits();
    })
    function getHits() {
        if(btn == 0){
            $.ajax({
                url: '/SyStatistical/getCourseClass',
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    var courseClass = data.data;
                    setVal(courseClass);
                }
            });
        }else if(btn == 1){
            $.ajax({
                url: '/SyStatistical/getCourseClassShare',
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    var courseClass = data.data;
                    setVal(courseClass);
                }
            });
        }else if(btn == 2){
            $.ajax({
                url: '/SyStatistical/getCourseClassColl',
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    var courseClass = data.data;
                    setVal(courseClass);
                }
            });
        }else if(btn == 3){
            $.ajax({
                url: '/SyStatistical/getCourseClassLook',
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    var courseClass = data.data;
                    setVal(courseClass);
                }
            });
        }
    }
    function setVal(data){
        categroys = [];
        titles = [];
        hits = [];
        var count;
        if(data.length > 5){
            count = 5;
        }else {
            count = data.length;
        }
        $('#product').text('');
        $('#product').append('<td>' + '课程类型' + '</td>');
        $('#hits').text('');
        if(btn == 0){
            $('#hits').append('<td>' + '点赞次数' + '</td>');
        }else if(btn == 1){
            $('#hits').append('<td>' + '分享次数' + '</td>');
        }else if(btn == 2){
            $('#hits').append('<td>' + '收藏次数' + '</td>');
        }else if(btn == 3){
            $('#hits').append('<td>' + '预览次数' + '</td>');
        }
        for (var i = 0; i < count; i++) {
            if(data[i].hits == null){
                data[i].hits = 0;
            }
            $('#product').append('<td>' + data[i].name + '</td>');
            $('#hits').append('<td>' + data[i].hits + '</td>');
            titles[i] = data[i].name;
            hits[i] = data[i].hits;
            var cate = {value:hits[i],name:titles[i]}
            categroys[i] = cate;
            circular();
        }
    }
    //刷新
    refresh=function refresh(){
        location.reload();
    }
    //返回按钮
    $('#goBack').click(function () {
        var title = $('#title').text();
        var iframe = parent.$('.iframe');
        $.each(iframe,function (i,item) {
            if(title == $(item).attr('_href')){
                parent.$('#beforeHref').val($(item).attr('_href'));
                parent.beforeHref();
                return;
            }
        });
    });
    //统计图刷新
    reload = function reload(){
        getHits()
    }
})

var option;
//扇形统计图
function circular(){
    var chartDom = document.getElementById('main');
    var myChart = echarts.init(chartDom);
    option = {
        series: [
            {
                type: 'pie',
                data: categroys,
                roseType: 'area'
            }
        ],
        xAxis: {
            show: false
        },
        yAxis: {
            show: false
        }
    };
    option && myChart.setOption(option);
}
//折线统计图
function strip() {
    var chartDom = document.getElementById('main');
    var myChart = echarts.init(chartDom);
    //var option;
    option = {
        xAxis: {
            show: true,
            type: 'category',
            data: titles
        },
        yAxis: {
            show: true,
            type: 'value'
        },
        series: [
            {
                data: hits,
                type: 'line'
            }
        ]
    };
    option && myChart.setOption(option);
}
//柱状统计图
function column(){
    var chartDom = document.getElementById('main');
    var myChart = echarts.init(chartDom);
    //var option;
    option = {
        xAxis: {
            show: true,
            data: titles
        },
        yAxis: {show: true},
        series: [
            {
                type: 'bar',
                data: hits
            }
        ]
    };
    option && myChart.setOption(option);
}

//下载图表
function downLoad(){
    var canvas = document.getElementsByTagName("canvas")
    if (canvas && canvas.length > 0) {
        var oA = document.createElement("a")
        oA.download = "Echarts图表" + ".png"
        oA.href = canvas[0].toDataURL("image/png")
        document.body.appendChild(oA)
        oA.click()
        oA.remove()
    }
}

